package oocl.eshop.ui.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oocl.eshop.po.Food;
import oocl.eshop.service.FoodManager;
import oocl.eshop.service.impl.FoodManagerImpl;

/**
 * Servlet implementation class AddFoodServlet
 */
public class AddFoodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	FoodManager foodManager = new FoodManagerImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddFoodServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Food f = (Food) request.getAttribute("thisFood");
		System.out.println("this"+f);
		if(f ==null)
			request.getRequestDispatcher("menuPage.jsp").forward(request, response);
		int i = foodManager.addFood(f);
		if (i == 0) {
			System.out.println("Food add Error");
		}
		HttpSession session = request.getSession();
		session.setAttribute("foods", foodManager.findFoodBymid(f.getmId()));
		request.getRequestDispatcher("showFoodPage").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
